package Tree;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class _872_Leaf_SimilarTrees {
    List<Integer> res = new ArrayList<>();
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        preorderTraversal(root1);
        List<Integer> ans1 = new ArrayList<>(res);
        res.clear();
        preorderTraversal(root2);
        List<Integer> ans2 = new ArrayList<>(res);
        if (ans1.size() != ans2.size()) {
            return false;
        } else {
            for (int i = 0; i < ans1.size(); i++) {
                if (ans1.get(i) != ans2.get(i)) {
                    return false;
                }
            }
        }
        return true;
    }

    public void preorderTraversal(TreeNode root) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            res.add(root.val);
        } else {
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
    }
}
